package com.lewis.module.m2.api.record.controller;

import com.lewis.api.system.entity.BotConfigFunction;
import com.lewis.api.system.entity.BotQq;
import com.lewis.api.system.vo.GetOneVO;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.entity.page.PageResult;
import com.lewis.common.core.utils.SpringUtil;
import com.lewis.common.mongodb.MongodbUtil;
import com.lewis.module.m2.api.bot.service.IBotQqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bson.types.ObjectId;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 机器人功能配置控制器
 *
 * @author Lewis 398529803@qq.com 2022年8月9日, 009 15:24:43
 */
@Api(tags = "qq机器人")
@RestController
@RequestMapping("/record/qq/bot/config/function")
public class RecordBotConfigFunctionController {

	@Resource
	private MongodbUtil mongodbUtil;

	@Resource
	private IBotQqService botQqService;

	@ApiOperation("根据机器人id获取机器人功能配置")
	@GetMapping("/getByBotId")
	public PageResult getByBotId(Long id) {
		BotQq botQq = botQqService.getById(id);
		if (botQq == null) {
			return PageResult.fail().setMsg("不存在该机器人信息");
		}
		return getByBotQq(Long.parseLong(botQq.getQq()));
	}

	@ApiOperation("获取机器人功能配置")
	@GetMapping("/getById")
	public BaseResult getById(String id) {
		return SpringUtil.getBean(RecordBotConfigFunctionController.class).getOne(new GetOneVO(id));
	}

	@ApiOperation("根据机器人qq获取功能配置")
	@GetMapping("/getByBotQq")
	public PageResult getByBotQq(Long botQq) {
		List<?> botQqList = mongodbUtil.getListByQuery(mongodbUtil.createQuery("botQq", botQq), BotConfigFunction.class);
		return PageResult.ok().setData(botQqList).setTotal(botQqList.size());
	}

	@ApiOperation("获取配置详情")
	@GetMapping("/getOne")
	public BaseResult getOne(GetOneVO vo) {
		if (vo.getId() != null) {
			return BaseResult.ok().setData(mongodbUtil.getOneById(vo.getId(), BotConfigFunction.class));
		}
		if (vo.getBotQq() != null && vo.getFunctionName() != null) {
			HashMap<String, Object> paramMap = new HashMap<>();
			paramMap.put("botQq", vo.getBotQq());
			paramMap.put("functionName", vo.getFunctionName());
			return BaseResult.ok().setData(mongodbUtil.getOneByQuery(mongodbUtil.createQuery(paramMap), BotConfigFunction.class));
		}
		if (vo.getBotQq() != null && vo.getFunctionName() == null) {
			HashMap<String, Object> paramMap = new HashMap<>();
			paramMap.put("botQq", vo.getBotQq());
			return BaseResult.ok().setData(mongodbUtil.getOneByQuery(mongodbUtil.createQuery(paramMap), BotConfigFunction.class));
		}
		return BaseResult.fail().setMsg("参数不完整，无法查询");
	}

	@ApiOperation("添加机器人功能配置")
	@PostMapping("/add")
	public BaseResult add(@RequestBody BotConfigFunction botConfigFunction) {
		BotQq botQq = botQqService.getById(botConfigFunction.getBotQq());
		if (botQq == null) {
			return BaseResult.fail().setMsg("机器人不存在账户信息");
		}
		botConfigFunction.setBotQq(Long.parseLong(botQq.getQq()));
		botConfigFunction.setId(new ObjectId().toString());
		return BaseResult.ok().setData(mongodbUtil.save(botConfigFunction));
	}

	@ApiOperation("批量新增机器人功能配置")
	@PostMapping("/addBatch")
	public BaseResult addBatch(@RequestBody BotConfigFunction botConfigFunction) {
		// 所有机器人列表
		List<BotQq> botQqList = botQqService.list();
		for (BotQq botQq : botQqList) {
			botConfigFunction.setBotQq(Long.parseLong(botQq.getQq()));
			add(botConfigFunction);
		}
		return BaseResult.ok();
	}

	@ApiOperation("修改机器人功能配置")
	@PostMapping("/update")
	public BaseResult update(@RequestBody BotConfigFunction botConfigFunction) {
		BotQq botQq = botQqService.getById(botConfigFunction.getBotQq());
		if (botQq == null) {
			return BaseResult.fail().setMsg("机器人不存在账户信息");
		}
		botConfigFunction.setBotQq(Long.parseLong(botQq.getQq()));
		return BaseResult.ok().setData(mongodbUtil.save(botConfigFunction));
	}

	@ApiOperation("批量删除机器人功能配置")
	@PostMapping("/delete/{ids}")
	public BaseResult delete(@PathVariable String[] ids) {
		return mongodbUtil.deleteByIds(ids, BotConfigFunction.class) ? BaseResult.ok() : BaseResult.fail();
	}

	@ApiOperation("根据botQq批量删除机器人功能配置")
	@PostMapping("/deleteBatch/{botQq}")
	public BaseResult deleteBatch(@PathVariable Long botQq) {
		mongodbUtil.deleteBatch(mongodbUtil.createQuery("botQq", botQq), BotConfigFunction.class);
		return BaseResult.ok();
	}

	@ApiOperation("根据机器人QQ清空所有配置")
	@PostMapping("/clear/{botQq}")
	public BaseResult clear(@PathVariable Long botQq) {
		BotQq botQqServiceById = botQqService.getById(botQq);
		if (botQqServiceById == null) {
			return BaseResult.fail().setMsg("机器人信息不存在");
		}
		return deleteBatch(Long.parseLong(botQqServiceById.getQq()));
	}

	@ApiOperation("从指定机器人中导入所有功能配置")
	@PostMapping("/importConfig/{fromBotQqId}/{toBotQqId}")
	public BaseResult importConfig(@PathVariable Long fromBotQqId, @PathVariable Long toBotQqId) {
		BotQq fromBotQq = botQqService.getById(fromBotQqId);
		BotQq toBotQq = botQqService.getById(toBotQqId);
		List<?> fromBotQqList = mongodbUtil.getListByQuery(mongodbUtil.createQuery("botQq", Long.parseLong(fromBotQq.getQq())), BotConfigFunction.class);
		for (Object object : fromBotQqList) {
			BotConfigFunction toBotConfigFunction = (BotConfigFunction) object;
			toBotConfigFunction.setId(new ObjectId().toString());
			toBotConfigFunction.setBotQq(Long.parseLong(toBotQq.getQq()));
			add(toBotConfigFunction);
		}
		return BaseResult.ok();
	}
}
